package com.wenda.homeschooedu.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.wenda.homeschooedu.DB.DB;
import com.wenda.homeschooedu.DB.DB.Table;
import com.wenda.homeschooedu.DB.JDBCConnection;
import com.wenda.homeschooedu.dao.Icontactor;
import com.wenda.homeschooedu.model.Parent;
import com.wenda.homeschooedu.model.Student;

public class ContactorService implements Icontactor {
	JDBCConnection jdbcConnection;
	Statement statement;

	public ContactorService() {
		jdbcConnection = new JDBCConnection();
		statement = jdbcConnection.getStm();
	}

	public List<Parent> getParentAll(int class_id) {
		String sql = String.format(Table.Parent.SQL.SELECT, class_id);
		List<Parent> parents = new ArrayList<Parent>();
		try {
			ResultSet resultSet = statement.executeQuery(sql);
			
			if (resultSet != null) {
				while (resultSet.next()) {
					Parent parent = new Parent();
					parent.set_id(resultSet.getInt(resultSet
							.findColumn("parent_id")));
					parent.setAddress(resultSet.getString(resultSet
							.findColumn("address")));
					parent.setName(resultSet.getString(resultSet
							.findColumn("parent_name")));
					parent.setPhone(resultSet.getString(resultSet
							.findColumn("parent_phone")));
					parent.setStudent_id(resultSet.getInt(resultSet
							.findColumn("student_id")));
					parent.setUserName(resultSet.getString(resultSet
							.findColumn("parent_username")));
					parent.setStudent_name(resultSet.getString(resultSet
							.findColumn("student_name")));
					parents.add(parent);
				}

			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		return parents;
	}

	public List<Student> getStudentAll(int class_id) {
		String sql = String.format(DB.Table.Student.SQL.SELECT, class_id);
		List<Student> students = new ArrayList<Student>();
		try {
			ResultSet resultSet = statement.executeQuery(sql);
			if (resultSet != null) {
				while (resultSet.next()) {
					Student student = new Student();
					student.set_id(resultSet.getInt(resultSet.findColumn("_id")));
					student.setAddress(resultSet.getString(resultSet
							.findColumn("address")));
					student.setAge(resultSet.getInt(resultSet.findColumn("age")));
					student.setClass_id(resultSet.getInt(resultSet
							.findColumn("class_id")));
					student.setName(resultSet.getString(resultSet
							.findColumn("name")));
					student.setPhone(resultSet.getString(resultSet
							.findColumn("phone")));
					student.setSex(resultSet.getInt(resultSet.findColumn("sex")));
					student.setUserName(resultSet.getString(resultSet
							.findColumn("username")));
					students.add(student);
				}
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		return students;

	}

}
